class Counter {
    int count = 0;
    public synchronized void add() {
         count++;
    }
}
public class Demo {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                counter.add();
                System.out.println("t1 ++");
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                counter.add();
                System.out.println("t2 ++");
            }
        });
        t1.start();
        t2.start();
        Thread.sleep(2000);
        System.out.println(counter.count);
    }
}
